package servlet;

import domain.Cars01;
import domain.Student;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import util.TemplateEngineUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/CarServlet")
public class CarServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        TemplateEngine engine = TemplateEngineUtil.getTemplateEngine(req.getServletContext());
        WebContext context = new WebContext(req, resp, req.getServletContext());
        resp.setCharacterEncoding("utf-8");

        req.setCharacterEncoding("UTF-8");
        String name = req.getParameter("name");
        String velocity = req.getParameter("Velocity");
        String oil = req.getParameter("Oil");
        List Cars=new ArrayList();

        Cars01 car1=new Cars01();
        car1.setName(name);
        car1.setVelocity(Integer.parseInt(req.getParameter("Velocity")));
        car1.setOil(Integer.parseInt(req.getParameter("Oil")));
        Cars.add(car1);

        Cars01 car2=new Cars01();
        car2.setName("雷克萨斯");
        car2.setVelocity(500);
        car2.setOil(10);
        Cars.add(car2);

        Cars01 car3=new Cars01();
        car3.setName("本田");
        car3.setVelocity(300);
        car3.setOil(5);
        Cars.add(car3);

        //deliver students to showUsername.html
        req.setAttribute("Cars",Cars);
        engine.process("showCars.html",context,resp.getWriter());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
